from socket import *
import base64
subject = "send from lidaoyi's python script!"
contenttype = "text/plain"
msg = "\r\n happy new year!"
endmsg = "\r\n.\r\n" 
# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = "smtp.qq.com"
# Sender and reciever
# Fill in start
# sender = '********'
sender_addr = ""
auth_code = ""
reciever_addr = ""
# Fill in end

# Auth information (Encode with base64)
# Fill in start
address = base64.b64encode(sender_addr.encode()) + b'\r\n'
password = base64.b64encode(auth_code.encode()) + b'\r\n'
# Fill in end

# Create socket called clientSocket and establish a TCP connection with mailserver
#Fill in start
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 587))
#Fill in end
recv = clientSocket.recv(1024).decode()
print(recv)
if recv[:3] != '220':
    print('220 reply not received from server.')

# Send HELO command and print server response.
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024).decode()
print(recv1)
if recv1[:3] != '250':
    print('250 reply not received from server.')

# Auth
# Fill in start
recv1 = clientSocket.recv(1024).decode()
login_cmd = 'auth login\r\n'
clientSocket.send(login_cmd.encode())
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if(recv[:3] != '334'):
    print("login error")
    exit()

clientSocket.send(address)
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if(recv[:3] != '334'):
    print("login error")
    exit()

clientSocket.send(password)
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if(recv[:3] != '235'):
    print("login error")
    exit()

# Send MAIL FROM command and print server response.
# Fill in start
mail_from_cmd = 'Mail from:<'+ sender_addr + '>\r\n'
clientSocket.send(mail_from_cmd.encode())
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if(recv[:3] != '250'):
    print("sender error")
    exit()
# Fill in end

# Send RCPT TO command and print server response.
# Fill in start
rcpt_to_cmd = 'Rcpt to:<'+ reciever_addr + '>\r\n'
clientSocket.send(rcpt_to_cmd.encode())
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if(recv[:3] != '250'):
    print("reciever error")
    exit()
# Fill in end

# Send DATA command and print server response.
# Fill in start
data_cmd = 'data\r\n'
clientSocket.send(data_cmd.encode())
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if(recv[:3] != '354'):
    print("content error")
    exit()
# Fill in end

# Send message data.
# Fill in start
title_cmd = 'subject:'+ subject + '\r\n'
clientSocket.send(title_cmd.encode())
mail_type = 'contenttype:'+ subject + '\r\n'
clientSocket.send(mail_type.encode())
clientSocket.send(msg.encode())
# Fill in end

# Message ends with a single period.
# Fill in start

clientSocket.send(endmsg.encode())
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if(recv[:3] != '250'):
    print("ending error")
    exit()
# Fill in end

# Send QUIT command and get server response.
# Fill in start
quit_cmd = 'quit\r\n'
clientSocket.send(quit_cmd.encode())
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if(recv[:3] != '221'):
    print("quit error")
    exit()
# Fill in end
# Close connection
# Fill in start
clientSocket.close()
# Fill in end 